﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
using System.Data;

namespace Cashier.prints
{
    public class DataGridViewPrint
    {       
        /// <summary>
        /// 分页打印DataGridView
        /// </summary>
        /// <param name="dgv">需打印的DataGridView</param>
        /// <param name="x">顶点X坐标</param>
        /// <param name="y">顶点Y坐标</param>
        /// <param name="rowHeight">行高</param>
        /// <param name="isPrintHeader">是否打印表头</param>
        /// <param name="startRowIndex">打印起始行的索引</param>
        /// <param name="printWidth">打印纸张宽度 A4纸的尺寸的图像的像素是595×842</param>
        /// <param name="printHeight">打印纸张高度</param>
        /// <param name="e">打印功能对象</param>
        /// <returns>当前页最后打印一行的索引</returns>
        public static int PrintPages(PrintParameter print,  PrintPageEventArgs e)
        {
            try
            {
                float fweight = 0f;
                foreach (DataGridViewColumn column in print.Dgv.Columns)
                {
                    if (column.Visible)
                    {
                        fweight += column.FillWeight;
                    }
                }
                if (fweight < 100)
                {
                    print.PrintWidth = print.PrintWidth + (int)(print.PrintWidth * (100 - fweight) / 100);
                }

                //计算列宽
                float fillWeight = 0f;
                int cellWidth = 0;

                //计算打印行数
                int rowCount = 0;
                if (print.IsPrintSelectedRows)
                    rowCount = print.Dgv.SelectedRows.Count;
                else
                    rowCount = print.Dgv.Rows.Count;

                //定义画笔
                Pen rPen = new Pen(Brushes.Black);
                Rectangle rect = new Rectangle(print.Left, print.Top, 0, 0);

                //设置内容对齐方式
                StringFormatStyle sfs = new StringFormatStyle();
                StringFormat sf = new StringFormat();

                DataGridViewContentAlignment columnAlign = DataGridViewContentAlignment.MiddleLeft;
                //列头高
                int headerRowHeight = print.RowHeight;
                //画DataGridView列头
                if (print.IsPrintHeader && print.StartRowIndex < rowCount)
                {
                    String headerText = String.Empty;
                    Font headerFont = new Font("宋体", 9);

                    foreach (DataGridViewColumn column in print.Dgv.Columns)
                    {
                        if (column.Visible)
                        {
                            fillWeight = column.FillWeight;
                            cellWidth = (int)(fillWeight * print.PrintWidth / 100);

                            headerText = column.HeaderText;
                            columnAlign = column.HeaderCell.Style.Alignment;
                            headerRowHeight = column.HeaderCell.Size.Height;

                            //画矩形
                            rect = new Rectangle(rect.X + rect.Width, rect.Y, cellWidth, headerRowHeight);
                            e.Graphics.DrawRectangle(rPen, rect);

                            if (columnAlign == DataGridViewContentAlignment.MiddleLeft)
                                sf = sfs.LeftMiddle;
                            else if (columnAlign == DataGridViewContentAlignment.MiddleCenter)
                                sf = sfs.CenterMiddle;
                            else if (columnAlign == DataGridViewContentAlignment.MiddleRight)
                                sf = sfs.RightMiddle;
                            //画字
                            e.Graphics.DrawString(headerText, headerFont, Brushes.Black, rect, sf);
                        }
                    }
                }
                
                //打印内容
                if (print.StartRowIndex < rowCount)
                {
                    String cellContent = String.Empty;
                    Pen cPen = new Pen(Brushes.Black, 1);
                    Font cFont = new Font("宋体", 9);
                    for (int i = print.StartRowIndex; i < rowCount; i++)
                    {
                        DataGridViewRow row = null;
                        if (print.IsPrintSelectedRows)
                            row = print.Dgv.SelectedRows[i];
                        else
                            row = print.Dgv.Rows[i];

                        if (print.RowHeight == 0)
                            print.RowHeight = row.Height;
                        rect.X = print.Left;
                        rect.Width = 0;
                        int rectY = rect.Y + rect.Height;
                        foreach (DataGridViewColumn column in print.Dgv.Columns)
                        {
                            if (column.Visible)
                            {
                                fillWeight = column.FillWeight;
                                cellWidth = (int)(fillWeight * print.PrintWidth / 100);
                                columnAlign = column.DefaultCellStyle.Alignment;
                                //单元格内容
                                if (row.Cells[column.Index].Value != null)
                                    cellContent = row.Cells[column.Index].FormattedValue.ToString();
                                //内容对齐方式
                                if (columnAlign == DataGridViewContentAlignment.MiddleLeft)
                                    sf = sfs.LeftMiddle;
                                else if (columnAlign == DataGridViewContentAlignment.MiddleCenter)
                                    sf = sfs.CenterMiddle;
                                else if (columnAlign == DataGridViewContentAlignment.MiddleRight)
                                    sf = sfs.RightMiddle;

                                rect = new Rectangle(rect.X + rect.Width, rectY, cellWidth, print.RowHeight);
                                e.Graphics.DrawRectangle(cPen, rect);
                                e.Graphics.DrawString(cellContent, cFont, Brushes.Black, rect, sf);
                            }
                        }

                        print.StartRowIndex = i;
                        if (print.StartRowIndex < (rowCount - 1))
                        {
                            if ((rect.Y + print.RowHeight) > print.PrintHeight)
                            {
                                print.StartRowIndex += 1;
                                e.HasMorePages = true;
                                break;
                            }
                            else
                            {
                                print.StartRowIndex = 0;
                                e.HasMorePages = false;
                            }
                        }
                        else
                        {
                            print.StartRowIndex = 0;
                            e.HasMorePages = false;
                        }
                    }
                }
                else
                {
                    e.HasMorePages = false;
                    return print.StartRowIndex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return print.StartRowIndex;
        }
    }
}
